const path = require('path')
const resolve = (dir) => {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: './',
  // 解决vue-echarts 引人失败
  transpileDependencies: ['vue-echarts', 'resize-detector'],
  // assetsDir: 'static',
  configureWebpack: {
    devtool: process.env.NODE_ENV === 'development' ? 'source-map' : false
  },

  // 扩展webpack配置
  chainWebpack: (config) => {
    // 配置svg load
    const svgRule = config.module.rule('svg') // 找到svg-loader
    svgRule.uses.clear() // 清除已有的loader, 如果不这样做会添加在此loader之后
    svgRule.exclude.add(/node_modules/) // 正则匹配排除node_modules目录
    svgRule // 添加svg新的loader处理
      .test(/\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })

    // 配置路径别名
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@v', resolve('src/views'))
      .set('@c', resolve('src/components'))
      .set('@p', resolve('public'))
      .set('@u', resolve('src/utils'))

    // 配置外部静态资源库
    const cdn = {
      css: ['./css/d2c-gl-2.3.0.css', './css/d2c.1.4.1.css'],
      js: [
        'js/config.js',
        'js/filterConfig.js',
        'js/d2c.2.3.0.min.js',
        'js/turf.min.js',
        'js/d2c.helper-0.32.min.js',
      ],
    }

    config.externals(['d2c', 'config', 'turf'])

    config.plugin(`html`).tap((args) => {
      args[0].title = '2025重庆市城市体检信息平台'
      args[0].cdn = cdn
      return args
    })
  },
  // 开发服务器配置
  devServer: {
    port: 8080,
    proxy: {
      '/mock8080': {
        target: 'http://124.70.64.99:8080',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/mock7774': '',
        },
      },
      // '/mock': {
      //   target: 'http://192.168.3.22:7771',
      //   changeOrigin: true,
      //   secure: false,
      //   pathRewrite: {
      //     '/mock': '',
      //   },
      // },
      // '/mock': {
      //   target: 'http://192.168.4.173:7771',
      //   changeOrigin: true,
      //   secure: false,
      //   pathRewrite: {
      //     '/mock': '',
      //   },
      // },
      // '/mock': {
      //   target: 'http://192.168.3.49:7771',
      //   changeOrigin: true,
      //   secure: false,
      //   pathRewrite: {
      //     '/mock': '',
      //   },
      // },
      '/mock': {
        target: 'http://10.23.100.71:7771',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/mock': '',
        },
      },
      '/mock/asd': {
        target: 'http://10.23.8.14:7774',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/mock/asd': '',
        },
      },
      // '/mock8080': {
      //   target: 'http://124.70.64.99:8080',
      //   changeOrigin: true,
      //   secure: false,
      //   pathRewrite: {
      //     '/mock8080': '',
      //   },
      // },
      '/gaode': {
        target: 'http://tm.amap.com',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/gaode': '',
        },
      },
      '/city/api': {
        // target: 'https://chengyu.caupdwest.com:8000/city/api',
        //target: 'http://192.168.3.3:3111',
        // target: 'http://192.168.22.87:3116',
        // target: 'http://10.23.20.2:7111',
        target: 'http://172.30.164.41:7111',
        // target: 'https://chengyu.caupdwest.com:8000/city2025/api',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/city/api': '',
        },
      },
      '/city/local': {
        // target: 'https://chengyu.caupdwest.com:8000/city/api',
        // target: 'http://192.168.3.3:3111',
        target: 'http://192.168.22.87:3116',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '/city/local': '',
        },
      },
    },
  },
}
